package com.qst.itoffer.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.qst.itoffer.bean.Company;
import com.qst.itoffer.bean.Job;
import com.qst.itoffer.util.DBUtil;


@SuppressWarnings("ALL")
public class JobDao {

	/**
	 * 根据企业编号查询此企业的所有招聘职位
	 * 
	 * @param companyId
	 * @return
	 */
	public List<Job> getJobListByCompanyID(String companyId) {
		List<Job> list = new ArrayList<Job>();
		Connection conn = DBUtil.getConnection();
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			String sql = "SELECT * FROM tb_job WHERE company_id=?";
			pst = conn.prepareStatement(sql);
			pst.setInt(1,Integer.parseInt(companyId));
			rs = pst.executeQuery();
			while (rs.next()) {
				Job job = new Job();
				job.setJobId(rs.getInt("job_id"));
				job.setJobName(rs.getString("job_name"));
				job.setJobSalary(rs.getString("job_salary"));
				job.setJobArea(rs.getString("job_area"));
				job.setJobEnddate(rs.getTimestamp("job_endtime"));
				list.add(job);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.closeJDBC(rs, pst, conn);
		}
		return list;
	}

	/**
	 * 根据职位编号查询职位详细信息
	 * @param jobId
	 * @return
	 */
	public Job getJobByID(String jobId) {
		Job job = new Job();
		Connection conn = DBUtil.getConnection();
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			String sql = "SELECT tb_job.*,company_pic "
					+ "FROM tb_job "
					+ "INNER JOIN tb_company on tb_job.company_id =  tb_company.company_id "
					+ "WHERE job_id=?";
			pst = conn.prepareStatement(sql);
			pst.setInt(1,Integer.parseInt(jobId));
			rs = pst.executeQuery();
			while (rs.next()) {
				job.setJobId(rs.getInt("job_id"));
				job.setJobName(rs.getString("job_name"));
				job.setJobHiringnum(rs.getInt("job_hiringnum"));
				job.setJobSalary(rs.getString("job_salary"));
				job.setJobArea(rs.getString("job_area"));
				job.setJobDesc(rs.getString("job_desc"));
				job.setJobEnddate(rs.getTimestamp("job_endtime"));
				job.setJobState(rs.getInt("job_state"));
				Company company = new Company();
				company.setCompanyId(rs.getInt("company_id"));
				company.setCompanyPic(rs.getString("company_pic"));
				job.setCompany(company);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.closeJDBC(rs, pst, conn);
		}
		return job;
	}

}
